﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Channels;
using UsageMetering.Client.UsageMeteringServiceReference;

namespace UsageMetering.Client
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                //InvocateServiceViaCode();

                InvocateServiceViaConfiguration();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            Console.Read();
        }

        static void InvocateServiceViaCode()
        {
            Binding httpBinding = new WSHttpBinding();
            Binding tcpBinding = new NetTcpBinding();

            EndpointAddress httpAddress = new EndpointAddress("http://localhost:8888/UsageMetering");
            EndpointAddress tcpAddress = new EndpointAddress("net.tcp://localhost:9999/UsageMetering");

            Console.WriteLine("Invocate self-host UsageMetering service... ...");

            using (UsageMeteringServiceClient UsageMetering_http = new UsageMeteringServiceClient(httpBinding, httpAddress))
            {
                using (UsageMeteringServiceClient UsageMetering_tcp = new UsageMeteringServiceClient(tcpBinding, tcpAddress))
                {
                    try
                    {
                        Console.WriteLine("Begin to invocate UsageMetering service via http transport... ...");
                        Console.WriteLine("x + y = {2} where x = {0} and y = {1}", 1, 2, UsageMetering_http.Add(1, 2));

                        Console.WriteLine("Begin to invocate UsageMetering service via tcp transport... ...");
                        Console.WriteLine("x + y = {2} where x = {0} and y = {1}", 1, 2, UsageMetering_tcp.Add(1, 2));
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }
            }
        }

        static void InvocateServiceViaConfiguration()
        {
            Console.WriteLine("Invocate self-host UsageMetering service... ...");

            using (UsageMeteringServiceClient UsageMetering_http = new UsageMeteringServiceClient("WSHttpBinding_IUsageMeteringService"))
            {
                using (UsageMeteringServiceClient UsageMetering_tcp = new UsageMeteringServiceClient("NetTcpBinding_IUsageMeteringService"))
                {
                    try
                    {
                        Console.WriteLine("Begin to invocate UsageMetering service via http transport... ...");
                        Console.WriteLine("x + y = {2} where x = {0} and y = {1}", 1, 2, UsageMetering_http.Add(1, 2));

                        Console.WriteLine("Begin to invocate UsageMetering service via tcp transport... ...");
                        Console.WriteLine("x + y = {2} where x = {0} and y = {1}", 1, 2, UsageMetering_tcp.Add(1, 2));
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }
            }
        }
    }
}
